package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.service.ICategoryService;
import com.itheima.reggie.service.ISetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private ISetmealService setmealService;
    @Autowired
    private ICategoryService categoryService;

    @PostMapping
    public R save(@RequestBody SetmealDto setmealDto) {

        setmealService.saveWithDish(setmealDto);

        return R.success("保存套餐成功！！");
    }

    @GetMapping("/page")
    public R pageQuery(Integer page, Integer pageSize, String name) {
        log.info("分页查询");
        //创建分页工具
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        Page<SetmealDto> dtoPage = new Page<>();

        Map<String, Object> map = new HashMap<>();


        //添加条件
        LambdaQueryWrapper<Setmeal> lqw = Wrappers.lambdaQuery(Setmeal.class)
                //添加查询条件，根据name进行like模糊查询
                .like(StringUtils.isNotEmpty(name), Setmeal::getName, name)
                //添加排序条件，根据更新时间降序排列
                .orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(pageInfo, lqw);

//对象拷贝
        BeanUtils.copyProperties(pageInfo, dtoPage, "records");
        List<Setmeal> records = pageInfo.getRecords();

        List<SetmealDto> list = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            //对象拷贝
            BeanUtils.copyProperties(item, setmealDto);
            //分类id
            Long categoryId = item.getCategoryId();
            //根据分类id查询分类对象
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                //分类名称
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());

        dtoPage.setRecords(list);
        return R.success(dtoPage);
    }


    @DeleteMapping
    public R delete(@RequestParam List<Long> ids) {
        log.info("ids{}" + ids);

        //分析传的ids是否可以作为id删除
        //判断套餐是否应该直接删除?处于售卖不可卖
        //删除数据后对应菜品数据也要删除(逻辑删除)
        setmealService.removeWithDish(ids);

        return R.success("套餐删除成功");
    }


    @PostMapping("/status/{status}")
    public R upadteStatus(@PathVariable Integer status, @RequestParam List<Long> ids) {
        log.info("修改套餐启用禁用" + status);
        LambdaUpdateWrapper<Setmeal> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(Setmeal::getStatus, status).in(Setmeal::getId, ids);
        boolean update = setmealService.update(wrapper);
        if (update) {
            return R.success("操作成功");
        }
        return R.error("操作失败,请先停售");
    }


    @GetMapping("/{id}")
    public R selectOne(@PathVariable Long id) {
        log.info("开始按id查询");
        SetmealDto setmealDto = setmealService.selectById(id);
        return R.success(setmealDto);

    }

    @PutMapping
    public R update(@RequestBody SetmealDto setmealDto) {
        log.info("开始更新数据");
        setmealService.updateWithDishes(setmealDto);
        return R.success(null);
    }

    @GetMapping("/list")

    public R selectByCategoryId(Long categoryId, Integer status) {
        log.info("selectByCategoryId");

        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(categoryId != 0, Setmeal::getCategoryId, categoryId).eq(Setmeal::getStatus, status);
        List<Setmeal> list = setmealService.list(lqw);
        return R.success(list);
    }

}
